package com.cjjd.c.feature.mine.information;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.cjjd.c.R;
import com.cjjd.c.ShareKeys;
import com.cjjd.c.base.ActionBarFragment;
import com.cjjd.c.bean.ContactBean;
import com.cjjd.c.bean.ContactInfoBean;
import com.cjjd.c.bean.HttpResult;
import com.cjjd.c.http.CallbackImpl;
import com.cjjd.c.http.JsonUtils;
import com.cjjd.c.utils.SharePrefUtil;
import com.cjjd.c.utils.StringUtil;
import com.cjjd.c.utils.ToastUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * ----------------------------------
 * Created by xyd on 2017/8/18.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class ContactInforFragment extends ActionBarFragment {

    @Bind(R.id.et_name)
    EditText etName;
    @Bind(R.id.et_phone)
    EditText etPhone;
    @Bind(R.id.et_email)
    EditText etEmail;
    @Bind(R.id.ll_address)
    LinearLayout llAddress;
    @Bind(R.id.toggleButton)
    ToggleButton toggleButton;
    @Bind(R.id.tv_del)
    TextView tvDel;

    @Override
    public Object getTitleString() {
        return "常用联系人详情";
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_contact_info;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }

    @Override
    protected void initView() {
        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
        h1.put("uuid", Uuid);
        Call<HttpResult<ContactInfoBean>> call = service.getMemberContactDetail(JsonUtils.convertParam("getMemberContactDetail", h1));
        call.enqueue(new CallbackImpl<ContactInfoBean>() {
            @Override
            public void responseSuccess(Call<HttpResult<ContactInfoBean>> call, Response<HttpResult<ContactInfoBean>> response) {
                ContactBean bean = response.body().getData().memberContactModel;
                if (bean != null && bean.uuid != null) {
                    Uuid = bean.uuid;
                }
                etName.setText(bean.name);
                etPhone.setText(bean.mobile);
                etEmail.setText(bean.email);
                if (bean.getIdDefault() == 0) {
                    toggleButton.setChecked(false);
                } else {
                    toggleButton.setChecked(true);
                }
            }
        });

    }

    @Override
    public void initRightButton(Button rightButton) {
        super.initRightButton(rightButton);
        rightButton.setText("保存");
        rightButton.setVisibility(View.VISIBLE);
        rightButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateMemberContact();
            }
        });
    }

    private void updateMemberContact() {

        String name = etName.getText().toString().trim();
        String mobile = etPhone.getText().toString().trim();
        String email = etEmail.getText().toString().trim();
        String idDefault = toggleButton.isChecked()?"1":"0";

        if (TextUtils.isEmpty(name)){
            ToastUtil.showToast("请输入姓名");
            return;
        }
        if (!StringUtil.isMobile(mobile)){
            ToastUtil.showToast("请输入正确的手机号");
            return;
        }
        if (!StringUtil.isEmail(email)){
            ToastUtil.showToast("请输入正确的邮箱");
            return;
        }


        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
        h1.put("uuid", Uuid);
        h1.put("name", name);
        h1.put("mobile", mobile);
        h1.put("email", email);
        h1.put("isDefault", idDefault);
        Call<HttpResult<String>> call = service.updateMemberContact(JsonUtils.convertParam("updateMemberContact", h1));
        call.enqueue(new CallbackImpl<String>() {
            @Override
            public void responseSuccess(Call<HttpResult<String>> call, Response<HttpResult<String>> response) {
                ToastUtil.showToast("更新成功");
                initView();
            }
        });

    }

    private String Uuid = "";

    public void setUuid(String uuid) {
        this.Uuid = uuid;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.tv_del)
    public void onViewClicked() {
        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
        h1.put("uuids", Uuid);
        Call<HttpResult<String>> call = service.delMemberContact(JsonUtils.convertParam("delMemberContact", h1));
        call.enqueue(new CallbackImpl<String>() {
            @Override
            public void responseSuccess(Call<HttpResult<String>> call, Response<HttpResult<String>> response) {
                ToastUtil.showToast("删除成功");
                mContext.onBackPressed();
            }
        });
    }
}
